package day021.tcp.demo5;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    private static final Logger LOGGER = LoggerFactory.getLogger("TcpServer服务端");

    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9090);
        LOGGER.trace("服务器启动，监听端口：9090");
        while (true) {
            Socket server = ss.accept();
            LOGGER.trace(server.getInetAddress().getHostAddress() + "已连接");
            InputStream netInput = server.getInputStream();
            FileOutputStream fos = new FileOutputStream("D:\\ideaProjects\\javase_code\\study-practice\\src" +
                    "\\day021\\tcp\\upload\\123.jpg");

            IOUtils.copy(netInput, fos);
            LOGGER.trace("文件上传成功！");

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
            bw.write("文件上传成功！");

            bw.close();
            fos.close();
            netInput.close();
            server.close();
        }
    }
}
